package com.ruoyi.count.service.impl;

import com.ruoyi.count.domain.StudentScore;
import com.ruoyi.count.mapper.CountMapper;
import com.ruoyi.count.service.CountService;
import com.ruoyi.education.domain.Indicator;
import com.ruoyi.education.domain.Score;
import com.ruoyi.students.domain.Students;
import com.ruoyi.students.mapper.StudentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 尹欣雨
 * @verdion 520
 * 18:07
 */
@Service
public class CountServiceImpl implements CountService {

    @Autowired
    private CountMapper countMapper;


    /**
     * 根据年级和学校查询学生某月的成绩
     * 把分数和指标信息放到一个集合中进行返回
     * @param grade 年级
     * @param schoolId 学校id
     * @return 学生成绩列表
     */
    @Override
    public List<StudentScore> getStudentScores(String grade, String schoolId ,String month) {
        List<Score> scores = countMapper.findStudentsByGradeAndSchool(grade,schoolId,month);
        // 用于存储合并后的学生成绩
        Map<String, StudentScore> studentScoreMap = new HashMap<>();

        for (Score score : scores) {
            String studentId = score.getStudentID();
            String indicatorId = score.getIndicatorID();

            // 如果学生不存在，创建一个新的 StudentScore
            StudentScore studentScore = studentScoreMap.computeIfAbsent(studentId, id -> {
                StudentScore newScore = new StudentScore();
                newScore.setStudentId(id);
                newScore.setScores(new HashMap<>());
                return newScore;
            });

            // 将分数添加到该学生的分数映射中
            studentScore.getScores().put(indicatorId, score.getScore());
        }

        // 返回合并后的学生成绩列表
        return new ArrayList<>(studentScoreMap.values());
    }
}
